#pragma once
#include "pch.h"
#include "Mesh.h"
using namespace AutomatedSurfaceReconstruction;

CMesh::CMesh(QObject* vParent) : QObject(vParent)
{

}

std::string CMesh::__getSuffix(const std::string& vFilePath)
{
	return vFilePath.substr(vFilePath.find_last_of('.') + 1);
}

std::string CMesh::__getOnlyFileName(const std::string& vFileName, const std::string& vFileSuffix, const std::unordered_map<std::string, PCLMeshType::Ptr>& vFilePath2Mesh)
{
	std::string OnlyFileName = vFileName;
	int i = 0;

	if (vFileSuffix == "las")
	{
		while (vFilePath2Mesh.count(OnlyFileName))
		{
			OnlyFileName = vFileName.substr(0, vFileName.find_last_of('.')) + "(" + std::to_string(++i) + ")." + vFileSuffix;
		}
	}
	else
	{
		if (vFileSuffix == "obj")
		{
			while (vFilePath2Mesh.count(OnlyFileName))
			{
				OnlyFileName = vFileName.substr(0, vFileName.find_last_of('.')) + "(" + std::to_string(++i) + ")." + vFileSuffix;
			}
		}
	}
	return OnlyFileName;
}

void CMesh::onActionLoadMesh(std::vector<std::string> vFilePathSet, std::unordered_map<std::string, PCLMeshType::Ptr> vioFilePath2Mesh)
{

	foreach(std::string FilePath, vFilePathSet)
	{
		std::string FileSuffix = __getSuffix(FilePath);
		std::string FileName = __getOnlyFileName(FilePath, FileSuffix, vioFilePath2Mesh);
		PCLMeshType::Ptr PCLMesh = make_shared<PCLMeshType>();

		pcl::io::loadPolygonFileOBJ(FilePath, *PCLMesh);
		vioFilePath2Mesh.insert(make_pair(FileName, PCLMesh));
		emit FinishLoadMesh(FileName, PCLMesh);
	}
	emit FinishLoadAllMesh();
}

